<template>
    <el-container>
        <el-header>
            <el-row
                class="row"
                type="flex"
                justify="space-between"
                align="middle"
            >
                <el-col span="{2}">
                    <h1>学生管理系统</h1>
                </el-col>
                <el-col span="{2}">
                    <div>
                        <span>{{ user.username }}</span>
                        <el-button
                            type="primary"
                            icon="el-icon-upload2"
                            circle
                            @click="handleLogout"
                        ></el-button>
                    </div>
                </el-col>
            </el-row>
        </el-header>
        <el-container>
            <el-aside width="200px">
                <el-menu
                    default-active="2"
                    class="el-menu-vertical-demo"
                    background-color="#545c64"
                    text-color="#fff"
                    active-text-color="#ffd04b"
                    :router="true"
                >
                    <el-menu-item index="/manage/student">
                        <i class="el-icon-menu"></i>
                        <span slot="title">学生管理</span>
                    </el-menu-item>
                    <el-menu-item index="/manage/teacher">
                        <i class="el-icon-document"></i>
                        <span slot="title">老师管理</span>
                    </el-menu-item>
                    <el-menu-item index="/manage/class">
                        <i class="el-icon-setting"></i>
                        <span slot="title">班级管理</span>
                    </el-menu-item>
                </el-menu>
            </el-aside>
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
import {getToken} from '../services/user';
export default {
    data() {
        return {
            user: {},
        };
    },
    async beforeRouteEnter(to, from, next) {
        let res = await getToken();
        if(Object.keys(res).length == 0){
            next('/login');
        }else{
            next((vm) => {
                // 通过 `vm` 访问组件实例
                vm.user = res.data;
            });
            // next();
        }
        
    },

    methods: {
        handleLogout(){
            localStorage.clear('token');
            this.$router.push('/login');
        }
    },
};
</script>

<style>
h1 {
    margin: 0;
}
.row {
    height: 60px;
}
.el-header {
    background-color: #909399;
}
.el-menu,
.el-aside,
.el-container,
html,
body,
#app {
    height: 100%;
}
</style>